#include "utils.h"

ScopedSignalBlocker::ScopedSignalBlocker(QObject *pObject)
    : m_pObject(pObject)
{
    m_pObject->blockSignals(true);
}

ScopedSignalBlocker::~ScopedSignalBlocker()
{
    m_pObject->blockSignals(false);
}

QPixmap generateImageTumbnail( const QImage& image, int tumbWidth /*= 64*/, int tumbHeight /*= 64*/, const QRect& frameRect /*= QRect()*/ )
{
	QImage tumbnail(tumbWidth, tumbHeight, QImage::Format_ARGB32);
	QPainter tumbnailPainter(&tumbnail);
	QImage resizedTo64x64;

	if(frameRect.isNull())
	{
		resizedTo64x64 = image.scaled(64, 64, Qt::KeepAspectRatio, Qt::SmoothTransformation);
	}
	else
	{
		QImage frameImage(frameRect.width(), frameRect.height(), QImage::Format_ARGB32);
		QPainter frameImagePainter(&frameImage);
		frameImagePainter.drawImage(QPoint(0, 0), image, frameRect);

		resizedTo64x64 = frameImage.scaled(64, 64, Qt::KeepAspectRatio, Qt::SmoothTransformation);
	}

	tumbnailPainter.drawImage(0, 0, resizedTo64x64);
	return QPixmap::fromImage(tumbnail);
}
